package com.zbl.zblproject.erp.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zbl.zblproject.erp.entity.ErpGoods;
import com.zbl.zblproject.erp.entity.ErpWarehouse;
import com.zbl.zblproject.erp.entity.ErpWarehouseItem;
import com.zbl.zblproject.erp.mapper.*;
import com.zbl.zblproject.erp.service.ErpWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author youlanqiang
 * @version 1.0
 * @date 2018/10/10
 */

@Service
@Transactional
public class ErpWarehouseServiceImpl implements ErpWarehouseService {


    @Autowired
    private ErpWarehouseMapper erpWarehouseMapper;

    @Autowired
    private ErpWarehouseItemMapper erpWarehouseItemMapper;

    @Autowired
    private ErpGoodMapper erpGoodMapper;

    @Autowired
    private ErpCategoryMapper erpCategoryMapper;


    @Override
    public boolean updateErpWarehouse(ErpWarehouse erpWarehouse){
        return 1 == erpWarehouseMapper.updateById(erpWarehouse);
    }

    @Override
    public boolean addErpWarehouse(ErpWarehouse erpWarehouse) {
        QueryWrapper<ErpWarehouse> wrapper = new QueryWrapper<>();
        wrapper.eq("wh_name", erpWarehouse.getWhName())
                .eq("shelves_id", erpWarehouse.getShelvesId())
                .eq("goods_allocation", erpWarehouse.getGoodsAllocation());
        ErpWarehouse warehouse =  erpWarehouseMapper.selectOne(wrapper);
        if(warehouse != null){
            throw new RuntimeException("已存在相同编码的仓库货位!");
        }
        return 1 == erpWarehouseMapper.insert(erpWarehouse);
    }

    @Override
    public boolean deleteErpWarehouse(int id) {
        QueryWrapper<ErpWarehouseItem> wrapper = new QueryWrapper<>();
        wrapper.eq("wh_id", id);
        wrapper.eq("stock", 0);
        //如果该商品下存在商品不可以被删除
        List<ErpWarehouseItem> items = erpWarehouseItemMapper.selectList(wrapper);
        if(items.size() > 0){
            return false;
        }
        return 1 == erpWarehouseMapper.deleteById(id);
    }

    @Override
    public IPage<ErpWarehouse> page(String search, int current, int size) {
        Page page = new Page(current, size);
        return erpWarehouseMapper.selectPage(page, search.trim());
    }

    @Override
    public IPage<JSONObject> pageGroup(String search, int current, int size) {
        Page page = new Page(current, size);
        IPage<ErpWarehouse> pageEntity =  erpWarehouseMapper.selectPageGroup(page, search.trim());
        Page<JSONObject> pages = new Page<>();
        pages.setTotal(pageEntity.getTotal());
        pages.setCurrent(pageEntity.getCurrent());
        pages.setSize(pageEntity.getSize());
        pages.setPages(pageEntity.getPages());
        List<JSONObject> wares = new ArrayList<>();
        for(ErpWarehouse item : pageEntity.getRecords()){
            List<ErpWarehouse> list = erpWarehouseMapper.selectListByWarehouse(item.getWhName());


            Map<String, List<ErpWarehouse>> map = list.stream().collect(Collectors.groupingBy(ErpWarehouse::getShelvesId));
            JSONObject object = new JSONObject();
            object.put("name", item.getWhName());
            JSONArray array = new JSONArray();
            for(String key : map.keySet()){
                JSONObject child = new JSONObject();
                child.put("name", key);
                child.put("children", map.get(key));
                array.add(child);
            }
            object.put("children", array);
            wares.add(object);
        }
        pages.setRecords(wares);
        return pages;
    }

    @Override
    public List<String> houseware(int status) {
        return erpWarehouseMapper.selectHouseName(status);
    }


    @Override
    public List<ErpWarehouse> allHouseware(int status) {
        QueryWrapper<ErpWarehouse> wrapper = new QueryWrapper<>();
        wrapper.eq("enabling", status);
        List<ErpWarehouse> list = erpWarehouseMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<String> shelves(String name, int status) {
        return erpWarehouseMapper.selectShelves(name, status);
    }

    @Override
    public List<ErpWarehouse> goods(String whname, String shename, int status) {
        QueryWrapper<ErpWarehouse> wrapper = new QueryWrapper<>();
        wrapper.eq("wh_name", whname);
        wrapper.eq("shelves_id", shename);
        wrapper.eq("enabling", status);
        return erpWarehouseMapper.selectList(wrapper);
    }


    @Override
    public List<ErpWarehouse> searchGoods(String search) {
        return erpWarehouseMapper.selectListOfSearch(search);
    }


    @Override
    public List<ErpWarehouseItem> getItemByWhId(int id) {
        QueryWrapper<ErpWarehouseItem> wrapper = new QueryWrapper<>();
        wrapper.eq("wh_id", id);
        wrapper.ne("stock", 0);
        List<ErpWarehouseItem> items = erpWarehouseItemMapper.selectList(wrapper);
        items.forEach(x->{
            ErpGoods erpGood = erpGoodMapper.selectById(x.getGoodsId());
            erpGood.setErpCategory(erpCategoryMapper.selectById(erpGood.getCatId()));
            x.setErpGood(erpGood);
            x.setErpWarehouse(erpWarehouseMapper.selectById(x.getWhId()));

        });
        return items;
    }

    @Override
    public List<ErpWarehouseItem> getItemByGoodsId(int id) {

        List<ErpWarehouseItem> items = erpWarehouseItemMapper.selectItemByGoodsId(id);
        items.forEach(x->{
            x.setErpWarehouse(erpWarehouseMapper.selectById(x.getWhId()));
        });
        return items;
    }

    @Override
    public boolean updateStatusForWarehouse(int id) {
        ErpWarehouse warehouse = erpWarehouseMapper.selectById(id);
        if(warehouse == null){
            return false;
        }
        QueryWrapper<ErpWarehouseItem> wrapper = new QueryWrapper<>();
        wrapper.eq("wh_id", warehouse.getId()).ne("stock", 0);
        if(warehouse.getEnabling() == 1){
            warehouse.setEnabling(0);
            return erpWarehouseMapper.updateById(warehouse) == 1;
        }else {
            int count = erpWarehouseItemMapper.selectCount(wrapper);
            if(count == 0){
                warehouse.setEnabling(1);
                return erpWarehouseMapper.updateById(warehouse) == 1;
            }else{
                return false;
            }
        }
    }
}
